/**
 * Created by yys on 2016/11/20.
 * @description 产品模板
 */
$(function(){
});
/**
 * 品牌搜索
 */
$('#brand_search').click(function () {
    var brand_keyword=$("#brand_keyword").val();
    $.ajax({
        type: "POST",
        url: "/backstage/brand/search-brand",
        data: {'search':brand_keyword},
        success: function (msg) {
            if(msg.state=1){
                var html='';
                for(var a=0;a<msg['data'].length;a++) {
                    html+='<option value="' + msg['data'][a].brand_id + '">'+ msg['data'][a].brand_name +'</option><br />';
                }
                $("#brand_search_res").html(html);
            }
        }, error: function () {
            swal({title: "系统繁忙！", text: "",type: "error", timer:1000 })
        }
    });
});
/**
 * 产品模板分类搜索
 */
$('#prod_temp_category_search').click(function () {
    var keyword=$("#prod_temp_category_keyword").val();
    $.ajax({
        type: "POST",
        url: "/backstage/product-temp/search-temp-category",
        data: {'search':keyword},
        success: function (msg) {
            if(msg.state=1){
                var html='';
                for(var a=0;a<msg['data'].length;a++) {
                    html+='<option value="' + msg['data'][a].id + '">'+ msg['data'][a].name +'</option><br />';
                }
                $("#prod_temp_category_search_res").html(html);
            }
        }, error: function () {
            swal({title: "系统繁忙！", text: "",type: "error", timer:1000 })
        }
    });
});
/***
 * 人群搜索
 */
$('#crowd_search').click(function () {
    var keyword=$("#temp_crowd_keyword").val();
    keyword=trim(keyword);
    $.ajax({
        type: "POST",
        url: "/backstage/product-temp/search-crowd",
        data: {'search':keyword},
        success: function (msg) {
            if(msg.state=1){
                var html='';
                for(var a=0;a<msg['data'].length;a++) {
                    html+='<option value="' + msg['data'][a].id + '">'+ msg['data'][a].crowd_name +'</option><br />';
                }
                $("#crowd_search_res").html(html);
            }
        }, error: function () {
            swal({title: "系统繁忙！", text: "",type: "error", timer:1000 })
        }
    });
});
$('#colour_search').click(function () {
    var keyword=$("#temp_colour_keyword").val();
    keyword=trim(keyword);
    $.ajax({
        type: "POST",
        url: "/backstage/product-temp/search-colour",
        data: {'search':keyword},
        success: function (msg) {
            if(msg.state=1){
                var html='';
                for(var a=0;a<msg['data'].length;a++) {
                    html+='<option value="' + msg['data'][a].id + '">'+ msg['data'][a].colour_name +'</option><br />';
                }
                $("#colour_search_res").html(html);
            }
        }, error: function () {
            swal({title: "系统繁忙！", text: "",type: "error", timer:1000 })
        }
    });
});
$('#size_search').click(function () {
    var keyword=$("#temp_size_keyword").val();
    var prod_class_id=$(".prod_temp_category_id").val();
    keyword=trim(keyword);
    console.log(prod_class_id);
    if(prod_class_id==0){
        swal({title: "请先选择产品类别！", text: "",type: "error", timer:1000 });
        return false;
    }
    $.ajax({
        type: "POST",
        url: "/backstage/product-temp/search-size",
        data: {'search':keyword,'prod_class_id':prod_class_id},
        success: function (msg) {
            if(msg.state=1){
                var html='';
                for(var a=0;a<msg['data'].length;a++) {
                    html+='<option value="">'+ msg['data'][a] +'</option><br />';
                }
                $("#size_search_res").html(html);
            }
        }, error: function () {
            swal({title: "系统繁忙！", text: "",type: "error", timer:1000 })
        }
    });
});
/**
 * 去除字符串前后空格
 * */
function trim(s){
    return s.replace(/(^\s*)|(\s*$)/g, "");
}
/***
 * 添加品牌
 */
function addBrand()
{
    var selText=$("#brand_search_res option:selected").text();
    var selVal=$("#brand_search_res option:selected").val();
    if(!empty(selText)){
        $('#brand_name').val(selText);
    }
    if(!empty(selVal)){
        $('#brand_id').val(selVal);
    }
}
/***
 * 添加产品模板分类
 */
function addProdTempCategory()
{
    var selText=$("#prod_temp_category_search_res option:selected").text();
    var selVal=$("#prod_temp_category_search_res option:selected").val();
    if((selText=='')||(typeof selVal=='undefined')){
        return false;
    }
    if(!empty(selText)){
        $('.prod_temp_category').val(selText);
    }
    if(!empty(selVal)){
        $('.prod_temp_category_id').val(selVal);
    }
}
/***
 * 添加人群
 */
function addProdTempCrowd()
{
    var selText=$("#crowd_search_res option:selected").text();
    var selVal=$("#crowd_search_res option:selected").val();
    var flag=true;
    if((selText=='')||(typeof selVal=='undefined')){
        return false;
    }
    $("#selected-crowd-div input").each(function (){
        var val = $(this).val();
        if(val==selVal){
            flag=false;
        }
    });
    if(flag){
        $('#selected-crowd-div').append('<input name="crowd_ids[]" type="checkbox" value="'+selVal+'" checked="checked" />'+selText+'</br>');
    }
}
function addProdTempColour()
{
    var selText=$("#colour_search_res option:selected").text();
    var selVal=$("#colour_search_res option:selected").val();
    var flag=true;
    if((selText=='')||(typeof selVal=='undefined')){
        return false;
    }
    $("#selected-colour-div input").each(function (){
        var val = $(this).val();
        if(val==selVal){
            flag=false;
        }
    });
    if(flag){
        $('#selected-colour-div').append('<input name="colour_ids[]" type="checkbox" value="'+selVal+'" checked="checked" />'+selText+'</br>');
    }
}
function addProdTempSize()
{
    var selText=$("#size_list option:selected").text();
    var flag=true;
    if(selText==''){
        return false;
    }
    $("#selected-size-div input").each(function (){
        var val = $(this).val();
        if(val==selText){
            flag=false;
        }
    });
    if(flag){
        $('#selected-size-div').append('<input name="sizes[]" type="checkbox" value="'+selText+'" checked="checked" />'+selText+'</br>');
    }
}
/***
 *
 * @param options
 * @param index
 */
function resultChange(options,index) {
    for(var i=0; i<options.length; i++){
        if(i==index){
            options[i].selected = true;
        }else{
            options[i].selected = false;
        }
    }
}
/***
 * 判断字符串是不是为空
 * @param exp
 * @returns {boolean}
 */
function empty(exp) {
    exp=trim(exp);
    if (!exp || typeof(exp)=="undefined" && exp=='')
    {
        return true;
    }
    return false;
}
/****
 * 标签搜索
 */
$('#tag_search').click(function () {
    var keyword=$("#tag_search_keyword").val();
    keyword=trim(keyword);
    $.ajax({
        type: "POST",
        url: "/backstage/tag/search-tag",
        data: {'search':keyword,'app_id':2,'type':1},
        success: function (msg) {
            if(msg.state=1){
                var html='';
                for(var a=0;a<msg['data'].length;a++) {
                    html+='<option value="' + msg['data'][a].id + '">'+ msg['data'][a].name +'(Id:'+ msg['data'][a].id +')</option><br />';
                }
                $("#tag_search_res").html(html);
            }
        }, error: function () {
            swal({title: "系统繁忙！", text: "",type: "error", timer:1000 })
        }
    });
});
function addTag()
{
    var selText=$("#tag_search_res option:selected").text();
    var selVal=$("#tag_search_res option:selected").val();
    var flag=true;
    if((selText=='')||(typeof selVal=='undefined')){
        return false;
    }
    $("#selected-tag-div input").each(function (){
        var val = $(this).val();
        if(val==selVal){
            flag=false;
        }
    });
    if(flag){
        $('#selected-tag-div').append('<input name="p_tag_ids[]" type="checkbox" value="'+selVal+'" checked="checked" />'+selText+'</br>');
    }
}